<?php
/**
 *   Joomdiet - administrator/components/models/recipecategory.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class JoomdietModelRecipeCategory extends JModel
{
    var $_recipecategory;
    
    function __construct()
    {
        parent::__construct();
        
        $array = JRequest::getVar('cid', '0', 00, 'array');
        $this->setId((int) $array[0]);
    }
    
    function setId($id)
    {
        $this->_id = $id;
        $this->_recipecategory = null;
    }
    
    function get($property, $default=null)
    {
        if ($this->_loadRecipeCategory()) {
            if (isset($this->_recipecategory->$property)) {
                return $this->_recipecategory->$property;
            }
        }
        return $default;
    }
    
    function &getRecipeCategory()
    {
        if ($this->_loadRecipeCategory()) {
            // do nothing
        } else $this->_initRecipeCategory();
        
        return $this->_recipecategory;
    }
    
    function _loadRecipeCategory()
    {
        if (empty($this->_recipecategory)) {
            $query = 'SELECT * FROM #__joomdiet_Recipes_Category'
                .' WHERE id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_recipecategory = $this->_db->loadObject();
            
            return (boolean) $this->_recipecategory;
        }
    }
    
    function _initRecipeCategory()
    {
        if (empty($this->_recipecategory)) {
            $recipecategory = new stdClass();
            $recipecategory->id = 0;
            $recipecategory->name = null;
            $recipecategory->description = null;
            $recipecategory->parent_category = null;
            
            $this->_recipecategory = $recipecategory;
            return (boolean) $this->_recipecategory;
        }
        return true;
    }
    
    function store()
    {
        $row =& JTable::getInstance('recipecategory', 'Table');

        $id = JRequest::getString('recipecategory_id');
        $name = JRequest::getString('recipecategory_name');
        $description = JRequest::getString('recipecategory_description');
        $parent_category = JRequest::getString('recipecategory_parent_category');
        
        if ($id) $row->set('id', $id);
        $row->set('name', $name);
        if ($parent_category > 0) {
            $row->set('parent_category', $parent_category);
        }
        $row->set('description', $description);
        
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        if (!$row->store()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        
        return true;
    }
    
    var $_data = array();
    
    // default query constructor
    function _buildQuery() {
        $query = 'SELECT * FROM #__joomdiet_Recipes_Category';
        return $query;
    }
    
    function getData()
    {
        
        if (empty($_data)) {
            $query = $this->_buildQuery();
            $elements = $this->_getList($query);
        }
        
        foreach ($elements as $element) {
            $node = $this->_createNode($element);
            $this->_insertNode($this->_data, $element->parent_category, $node);
        }
        
        return $this->_data;
    }
    
    function _createNode($element=null)
    {
        $node = array(
            'data' => $element,
            'children' => array(),
        );
        
        return $node;
    }
    
    function _insertNode(&$root, $parent, $node)
    {
        $found = false;
        if ($node['data']->parent_category == '') {
            $this->_data[] = $node;
        } else {
            foreach ($root as &$n) {
                if ($n['data']->id == $parent) {
                    array_push($n['children'], $node);
                } else {
                    if ($this->isLeaf($n)) {
                        // do nothing, recursive break condition
                    } else $this->_insertNode($n['children'], $parent, $node);
                }
            }
        }
    }
    
    function isLeaf($node)
    {
        return empty($node['children']);
    }
}
?>
